package cn.easybuy.service.impl;

import java.sql.Connection;
import java.util.List;

import cn.easybuy.dao.CategoryDao;
import cn.easybuy.dao.impl.CategoryDaoImpl;
import cn.easybuy.entity.Category;
import cn.easybuy.entity.Page;
import cn.easybuy.service.CategoryService;
import cn.easybuy.utils.DataSourceUtil;

public class CategoryServiceImpl implements CategoryService{
	private  Connection conn;
	@Override
	public List<Category> findCategoryList(Page page) throws Exception {
		try {
			conn = DataSourceUtil.getConnection();
			CategoryDao dao = new CategoryDaoImpl(conn);
			return dao.findCategoryList(page);
		} catch (Exception e) {
			e.printStackTrace();
			throw e;
		}finally{
			DataSourceUtil.closeAll(conn, null, null);
		}
	}
	
	@Override
	public Category findCategoryList(int id) throws Exception {
		try {
			conn = DataSourceUtil.getConnection();
			CategoryDao dao = new CategoryDaoImpl(conn);
			return dao.findCategoryList(id);
		} catch (Exception e) {
			e.printStackTrace();
			throw e;
		}finally{
			DataSourceUtil.closeAll(conn, null, null);
		}
	}
	
	@Override
	public int CategoryTotalCount() throws Exception {
		try {
			conn = DataSourceUtil.getConnection();
			CategoryDao dao = new CategoryDaoImpl(conn);
			return dao.CategoryTotalCount();
		} catch (Exception e) {
			e.printStackTrace();
			throw e;
		}finally{
			DataSourceUtil.closeAll(conn, null,null);
		}
	}
	@Override
	public int deleteCategory(int id) throws Exception {
		try {
			conn = DataSourceUtil.getConnection();
			CategoryDao dao = new CategoryDaoImpl(conn);
			return dao.deleteCategory(id);
		} catch (Exception e) {
			e.printStackTrace();
			throw e;
		}finally{
			DataSourceUtil.closeAll(conn, null, null);
		}
	}
	
	@Override
	public int addCategory(Category category) throws Exception {
		try {
			conn = DataSourceUtil.getConnection();
			CategoryDao dao = new CategoryDaoImpl(conn);
			return dao.addCategory(category);
		} catch (Exception e) {
			e.printStackTrace();
			throw e;
		}finally{
			DataSourceUtil.closeAll(conn, null, null);
		}
	}
	
	@Override
	public List<Category> parentName(int type) throws Exception {
		try {
			conn = DataSourceUtil.getConnection();
			CategoryDao dao = new CategoryDaoImpl(conn);
			return dao.parentName(type);
		} catch (Exception e) {
			e.printStackTrace();
			throw e;
		}finally{
			DataSourceUtil.closeAll(conn, null, null);
		}
	}
	@Override
	public int updateCatory(Category category) throws Exception{
		try {
			conn = DataSourceUtil.getConnection();
			CategoryDao dao = new CategoryDaoImpl(conn);
			return dao.updateCatory(category);
		} catch (Exception e) {
			e.printStackTrace();
			throw e;
		}finally{
			DataSourceUtil.closeAll(conn, null, null);
		}
	}
	
}
